#include <QDebug>
#include "googletts.h"

GoogleTTS::GoogleTTS(QString language)
{
    m_url = "http://translate.google.com/translate_tts?ie=UTF-8&tl=%1&q=%2";
    m_language = language;
    m_playlist = new QMediaPlaylist;
    m_player = new QMediaPlayer;
    connect(m_player, SIGNAL(stateChanged(QMediaPlayer::State)),this,SLOT(PlayerFinished(QMediaPlayer::State)));
}

void GoogleTTS::setLanguage(QString language)
{
    m_language = language;
}




void GoogleTTS::Speak(QString text)
{
        text.replace(" ","+");
         ///m_playlist->addMedia(QUrl(m_url.arg(m_language).arg(text)));
        QStringList list =text.split(QRegExp("\\.|\\,|\\?|\\!|\\:|\\\n")); //-- use delimeters ',' ',' '?' '!' ':')
        for(int i=0;i<list.length();i++)
            m_playlist->addMedia(QUrl(m_url.arg(m_language).arg(list.at(i))));

        m_player->setPlaylist(m_playlist);
        m_player->setPlaybackRate(1.5);
        m_player->play();
}



void GoogleTTS::PlayerFinished(QMediaPlayer::State state)
{
    if (state == QMediaPlayer::StoppedState)
    {
        m_playlist->clear();
        emit SpeakingFinished();
    }
}








//void GoogleTTS::Speak(QString text)
//{
//    text.replace(" ","+");
//    sTextBuff.at(0)=text[0:4];
//    sTextBuff= text.split(QRegExp("\\.|\\?|\\!|\\:")); //-- use delimeters ',' '?' '!' ':'
//    iTextBuffIndex=0;

//    for(int i=0;i<sTextBuff.length();i++)
//    {
//        qDebug()<<"Zdanie "<<i<<": "<<sTextBuff.at(i);
//    }
//    qDebug()<<"Reading "<<iTextBuffIndex<<": "<<sTextBuff.at(iTextBuffIndex);
//    vPlayStatement(sTextBuff.at(iTextBuffIndex++));
//}

//void GoogleTTS::vPlayStatement(QString sStatement)
//{
//QString full_link;
//QUrl uLink;

//    uLink.setUrl((QString)LINK+sStatement);

////    full_link.append(LINK);
////    full_link.append(sStatement);
//    //full_link.append(QUrl::toPercentEncoding(sStatement));
////    qDebug()<<"Link"<<full_link.length()<<"Chars | "<<full_link;

//    qDebug()<<"Link: "<<uLink;
//    player.setVolume(100);
//    player.setMedia(uLink); //-- more info on: http://doc.qt.io/qt-5/qmediaplayer.html
//    //  player.setMedia(QUrl("http://www.freeinfosociety.com/media/sounds/2518.mp3"));
//    //--- simular usage
//        //player.setMedia(QUrl::fromLocalFile("D:/tts2.mp3"));
//        //player.setMedia(QUrl("http://www.tonycuffe.com/mp3/tailtoddle_lo.mp3"));
//        //player.setMedia(QUrl("http://download.wavetlan.com/SVV/Media/HTTP/MP4/ConvertedFiles/Media-Convert/Unsupported/test7.mp4"));
//    player.setPlaybackRate(1.5);
//    player.play();
//}

//void GoogleTTS::PlayerFinished(QMediaPlayer::State newState)
//{
//    if(newState==QMediaPlayer::StoppedState)
//    {
//        if(iTextBuffIndex<sTextBuff.length())
//        {
//            qDebug()<<"Reading "<<iTextBuffIndex<<": "<<sTextBuff.at(iTextBuffIndex);
//            vPlayStatement(sTextBuff.at(iTextBuffIndex++));
//        }
//    }
//}

GoogleTTS::~GoogleTTS()
{

}

